package com.zfjt.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.zfjt.constants.InterfaceDefinition;
import com.zfjt.entity.ReturnBean;
import com.zfjt.entity.UserBean;
import com.zfjt.packnum.PackNum00006;
import com.zfjt.service.UserService;

/**
 * 修改密码
 * @author xk
 * @version 1.0
 * @date 2017年3月30日
 */
@Controller
public class ChangePassController {
	
	private ReturnBean mReturn;
	
	@Autowired
	private UserService mUser;
	
	private Map<String, String> fhdata;
	
	@RequestMapping(value="00006")
	@ResponseBody
	public ReturnBean ChangePass(HttpSession hseesion){
		mReturn = new ReturnBean();
		fhdata = new HashMap<>();
		//传过来的参数
		String data =  (String) hseesion.getAttribute("data");
		PackNum00006 num00006 = JSON.parseObject(data,PackNum00006.class); 		
		if (data.equals(InterfaceDefinition.IStatusCode.TOKEN_FAILURE)) {//校验码错误
			mReturn.setCode("001");
			mReturn.setPortnum("00001");
			fhdata.put("error", "TOKEN_ERROR");
			mReturn.setData(JSON.toJSONString(fhdata));
		}else{
			UserBean CxUesr = mUser.selectByPrimaryKey(num00006.getAccount());
			List<UserBean> mData = new ArrayList<>();
			mData = mUser.selectAll();
			int is = 0;
			for (int i = 0; i < mData.size(); i++) {
				UserBean dg = mData.get(i);
				if (dg.getAccount().equals(num00006.getAccount())) {
					is = 1; //表示有此账户
				}
			}
			if (is == 1) {
				if (!CxUesr.getType().equals("0")) {//所属网点相同或账号为总部账号，可以修改
					if(num00006.getPassword().equals(num00006.getPassword_again())){
						CxUesr.setPassword(num00006.getPassword());
						mUser.updateByPrimaryKeySelective(CxUesr);
						mReturn.setCode("000");
						mReturn.setPortnum("00006");
						mReturn.setData("修改成功");
					}else{
						mReturn.setCode("002");
						mReturn.setPortnum("00006");
						fhdata.put("error", "PASS_ERROR");//两个密码不同，返回错误
						mReturn.setData(JSON.toJSONString(fhdata));
					}
				}else{
					mReturn.setCode("002");
					mReturn.setPortnum("00006");
					fhdata.put("error", "TYPE_ERROR");//权限不足
					mReturn.setData(JSON.toJSONString(fhdata));
				}
			}else{
				mReturn.setCode("002");
				mReturn.setPortnum("00006");
				fhdata.put("error", "NULL_ERROR");//无此账号
				mReturn.setData(JSON.toJSONString(fhdata));
			}
			
		}
		return mReturn;
	}
}
